// TryCapVideoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TryCapVideo.h"
#include "TryCapVideoDlg.h"
#include "ImageLibrary.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CTryCapVideoDlg dialog




CTryCapVideoDlg::CTryCapVideoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTryCapVideoDlg::IDD, pParent)
	, m_FrameWidth(1)
	, m_FrameHeight(1)
	, m_FrameRate(1)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTryCapVideoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_Width, m_FrameWidth);
	DDV_MinMaxLong(pDX, m_FrameWidth, 1, 10000);
	DDX_Text(pDX, IDC_EDIT_Height, m_FrameHeight);
	DDV_MinMaxLong(pDX, m_FrameHeight, 1, 10000);
	DDX_Text(pDX, IDC_EDIT_FrameRate, m_FrameRate);
	DDV_MinMaxLong(pDX, m_FrameRate, 1, 10000);
}

BEGIN_MESSAGE_MAP(CTryCapVideoDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_Start, &CTryCapVideoDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_Stop, &CTryCapVideoDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_Grab, &CTryCapVideoDlg::OnBnClickedButtonGrab)
	ON_BN_CLICKED(IDC_BUTTON_Configure, &CTryCapVideoDlg::OnBnClickedButtonConfigure)
	ON_BN_CLICKED(IDC_BUTTON_Display, &CTryCapVideoDlg::OnBnClickedButtonDisplay)
	ON_WM_HSCROLL()
END_MESSAGE_MAP()


// CTryCapVideoDlg message handlers

BOOL CTryCapVideoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	GetDlgItem(IDC_SLIDER_Brightness)->EnableWindow(FALSE);
	GetDlgItem(IDC_SLIDER_Contrast)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTryCapVideoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTryCapVideoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTryCapVideoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CTryCapVideoDlg::OnBnClickedButtonStart()
{
	// TODO: Add your control notification handler code here
	if(SUCCEEDED(m_capVdo.Open(0,GetDlgItem(IDC_Preview)->GetSafeHwnd(),GRAB_MODE_BUFFER,TRUE)))
	{
		GetDlgItem(IDC_SLIDER_Brightness)->EnableWindow(TRUE);
		GetDlgItem(IDC_SLIDER_Contrast)->EnableWindow(TRUE);

		long lMin,lMax,lStep,lDefault,lFlags,lValue;
		if(m_capVdo.GetRangeVideoAmpProperty(VideoProcAmp_Brightness,&lMin,&lMax,&lStep,&lDefault,&lFlags))
			((CSliderCtrl *)GetDlgItem(IDC_SLIDER_Brightness))->SetRange(lMin,lMax,TRUE);
		if(m_capVdo.GetVideoAmpProperty(VideoProcAmp_Brightness,&lValue,&lFlags))
			((CSliderCtrl *)GetDlgItem(IDC_SLIDER_Brightness))->SetPos(lValue);

		if(m_capVdo.GetRangeVideoAmpProperty(VideoProcAmp_Contrast,&lMin,&lMax,&lStep,&lDefault,&lFlags))
			((CSliderCtrl *)GetDlgItem(IDC_SLIDER_Contrast))->SetRange(lMin,lMax,TRUE);
		if(m_capVdo.GetVideoAmpProperty(VideoProcAmp_Contrast,&lValue,&lFlags))
			((CSliderCtrl *)GetDlgItem(IDC_SLIDER_Contrast))->SetPos(lValue);

		long wid,hei,rate;
		if(m_capVdo.GetFrameSize(&wid,&hei) && m_capVdo.GetFramePerSec(&rate))
		{
			m_FrameWidth=wid;
			m_FrameHeight=hei;
			m_FrameRate=rate;
			UpdateData(FALSE);
		}
	}
}

void CTryCapVideoDlg::OnBnClickedButtonStop()
{
	// TODO: Add your control notification handler code here
	m_capVdo.Close();
	GetDlgItem(IDC_SLIDER_Brightness)->EnableWindow(FALSE);
	GetDlgItem(IDC_SLIDER_Contrast)->EnableWindow(FALSE);
}

void CTryCapVideoDlg::OnBnClickedButtonGrab()
{
	// TODO: Add your control notification handler code here
	static int numGrabbed=0;
	long wid=0,hei=0,len=0;
	m_capVdo.GetFrameSize(&wid,&hei);
	len=m_capVdo.GetFrameLength();

	if(len!=wid*hei && len!=3*wid*hei)return;
	char *pBuffer=new char[len];
	m_capVdo.GrabOneFrame(pBuffer);
	numGrabbed++;
	CString filename;
	filename.Format(_T("E:\\dxGrab\\%d.bmp"),numGrabbed);

	if(len==wid*hei)
	{
		GrayImage img(wid,hei);
		for(int i=0;i<len;i++)img.bmp[i]=((IMDATA)pBuffer[i])/255.0;
		img.SaveImg(filename);
	}
	else
	{
		RgbImage img(wid,hei);
		for(int i=0;i<len;i++)img.bmp[i]=((IMDATA)pBuffer[i])/255.0;
		img.SaveImg(filename);
	}
	delete [] pBuffer;
}

void CTryCapVideoDlg::OnBnClickedButtonConfigure()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	long wid=m_FrameWidth,hei=m_FrameHeight,rate=m_FrameRate;
	if(!m_capVdo.SetFrameSize(wid,hei) || !m_capVdo.SetFramePerSec(rate))MessageBox(L"Some error...");
}

void CTryCapVideoDlg::OnBnClickedButtonDisplay()
{
	// TODO: Add your control notification handler code here
	long lMin,lMax,lStep,lDefault,lFlags;
	for(long lProperty=VideoProcAmp_Brightness;lProperty<=VideoProcAmp_Contrast;lProperty++)
	{
		m_capVdo.GetRangeVideoAmpProperty(lProperty,&lMin,&lMax,&lStep,&lDefault,&lFlags);
		m_capVdo.SetVideoAmpProperty(lProperty,lDefault,lFlags);
	}
}

void CTryCapVideoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	if(pScrollBar->GetSafeHwnd()==GetDlgItem(IDC_SLIDER_Brightness)->GetSafeHwnd())
	{
		long lFlags,lValue;
		m_capVdo.GetVideoAmpProperty(VideoProcAmp_Brightness,&lValue,&lFlags);
		lValue=((CSliderCtrl *)GetDlgItem(IDC_SLIDER_Brightness))->GetPos();
		m_capVdo.SetVideoAmpProperty(VideoProcAmp_Brightness,lValue,lFlags);
	}
	if(pScrollBar->GetSafeHwnd()==GetDlgItem(IDC_SLIDER_Contrast)->GetSafeHwnd())
	{
		long lFlags,lValue;
		m_capVdo.GetVideoAmpProperty(VideoProcAmp_Contrast,&lValue,&lFlags);
		lValue=((CSliderCtrl *)GetDlgItem(IDC_SLIDER_Contrast))->GetPos();
		m_capVdo.SetVideoAmpProperty(VideoProcAmp_Contrast,lValue,lFlags);
	}

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
